package cn.alterzhang.www.test;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created by zhangshuhan on 2019/1/31.
 */
public class MasterSelect {

    static CuratorFramework client = CuratorFrameworkFactory.newClient("198.181.39.68:2181", 5000, 3000, new ExponentialBackoffRetry(1000, 3));
    static String masterPath = "/curator_master_path";

    public static void main(String[] args) throws InterruptedException {
        client.start();
        LeaderSelector leaderSelector = new LeaderSelector(client, masterPath, new LeaderSelectorListenerAdapter() {
            @Override
            public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
                System.out.println("成为master角色");
                Thread.sleep(3000);
                System.out.println("完成master,释放master权利");
            }
        });
        leaderSelector.autoRequeue();
        leaderSelector.start();
        Thread.sleep(Integer.MAX_VALUE);

    }
}
